<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Article;
use app\models\Test;

class TestController extends Controller
{
	public function actionIndex()
	{
		// echo "Hello";
		// $this->redirect(['user-index']);
		// $this->goHome();

		// $model=new Article();
		// $re=Article::findOne(1);

		// $re=new Test();

		// var_dump(\yii::$app->request->isPost);
		// echo \yii::$app->request->userAgent,"<br />";
		// die;

		// return $this->renderPartial('index',['data'=>[1,2]]);


		if(\yii::$app->request->isPost){
			var_dump(\yii::$app->request->post());
			exit;
		}


		$model=new \app\models\Article;
		return $this->renderPartial('article',['model'=>$model]);
	}

	public function actionUserIndex()
	{
		echo "second";
	}

	public function actionValidate(){

		$data=[
			'Test'=>[
				'username'=>'wfeng',
				'password'=>'1234'
			],
		];
		$test=new \app\models\Test;
		$test->load($data);
		var_dump($test);
		die;

	}

	public function actionDb(){
		$article=Article::findAll(['status'=>'1']);
		print_r($article);
	}

	public function actionCs()
	{
		// $cookie=new \yii\web\Cookie([
		// 	'name'=>'wfeng',
		// 	'expire'=>time()+1000,
		// 	'httpOnly'=>true,
		// 	'value'=>'cookieValue'
		// ]);
		// \Yii::$app->response->getCookies()->add($cookie);

		$cookie=\Yii::$app->request->cookies;

	}


	public function actions()
	{
		return [
			'captcha' => [
				'class'=>'yii\captcha\captchaAction',
				'maxLength'=>4,
				'minLength'=>4,
				'width'=>80,
				'height'=>40
			],
		];
	}

	public function actionCode()
	{

		$model=new \app\models\Code;

		if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
			if ($model->validate()) {
				echo "验证成功";
			}else{
				var_dump($model->getErrors());
			}
		}
		return $this->render('code',['model'=>$model]);
	}

	public function actionCodeTwo()
	{
		if (Yii::$app->request->isPost) {
			$code=Yii::$app->request->post('code');
			$caValidate = new \yii\captcha\CaptchaValidator();
			$caValidate->captchaAction='test/captcha';
			$vaction = $caValidate->createCaptchaAction();
			$scode = $vaction->getVerifyCode();
			if ($code==$scode) {
				echo "验证成功";
			}else{
				echo "验证码不正确";
			}
		}
		return $this->render('codetwo',[]);
	}

	public function actionCache()
	{
		$cache = Yii::$app->cache;
		$cache->add('name','wfeng');
	}

	public function actionUpload(){
		$upload=new \app\models\Upload();
		if (\Yii::$app->request->isPost) {
			$upload->uploadFile= \yii\web\UploadedFile::getInstance($upload,'uploadFile');
			if ($upload->upload()) {
				echo "上传成功";
			}else{
				var_dump($upload->getErrors());
			}
		}

		return $this->render('upload',['upload'=>$upload]);
	}

	public function actionRbac(){
		$auth=\Yii::$app->authMessager;
		$perm=$auth->createPermission('demo-rbac');
		$perm->description='demo rbac';
		$auth->add($perm);
	}

}